package com.cskaoyan.controller;

import com.cskaoyan.bean.Role;
import com.cskaoyan.bo.CreateRoleBo;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.RoleListVo;
import com.cskaoyan.vo.RoleOptionsVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RoleControlller {

    @Autowired
    RoleService roleService;

    /**
     * 功能:管理员页面role/options
     * 作者:whj
     */
    @RequestMapping("admin/role/options")
    public BaseRespVo query() {
        List<RoleOptionsVo> roleOptionsVoList = roleService.query();
        return BaseRespVo.ok(roleOptionsVoList);
    }

    /**
     * 作者：grz
     * 功能：角色管理页面获取角色列表
     *
     * @return BaseRespVo
     */
    @RequestMapping("admin/role/list")
    public BaseRespVo list(Integer page, Integer limit, String name, String sort, String order) {
        RoleListVo list = roleService.list(page, limit, name, sort, order);
        return BaseRespVo.ok(list);
    }


    /**
     * 作者：grz
     * 功能：添加角色
     *
     * @return BaseRespVo
     */
    @RequestMapping("admin/role/create")
    public BaseRespVo create(@RequestBody CreateRoleBo createRoleBo) {

        Role role = roleService.create(createRoleBo);

        if (role == null) {
            return BaseRespVo.error("创建角色失败");
        }

        return BaseRespVo.ok(role);
    }

    /**
     * 作者：grz
     * 功能：删除角色
     * @return BaseRespVo
     */
    @RequestMapping("admin/role/delete")
    public BaseRespVo delete(@RequestBody Role role) {

        int i = roleService.delete(role);

        if (i == 404) {
            return BaseRespVo.error("删除角色失败");
        }

        return BaseRespVo.ok("删除角色成功");
    }

    /**
     * 作者：grz
     * 功能：修改角色信息
     * @return BaseRespVo
     */
    @RequestMapping("admin/role/update")
    public BaseRespVo update(@RequestBody Role role) {

        int i = roleService.update(role);

        if (i == 404) {
            return BaseRespVo.error("修改角色失败");
        }

        return BaseRespVo.ok("修改角色成功");
    }


}
